//
//  ShellRechargeViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/27.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellRechargeViewModel.h"
@interface ShellRechargeViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellRechargeViewModel

/**
Convert  充值地址
*/
- (void)getWalletAddress{
    
    NSString *coinCode = @"";
    switch (self.wallertModel.currencyType) {
        case ShellCurrencyTypeETH_USDT:{
            if(self.wallertModel.ethAddress.length > 0){
                [self.reloadSubject sendNext:@(3)];
                return;
            }
            coinCode = @"ETH_USDT";
        }
            break;
        case ShellCurrencyTypeETH:{
            coinCode = @"ETH";
            if(self.wallertModel.address.length > 0){
                [self.reloadSubject sendNext:@(3)];
                return;
            }
            
        }
            break;
        case ShellCurrencyTypeUSDT:{

              if(self.wallertModel.address.length > 0){
                  [self.reloadSubject sendNext:@(3)];
                  return;
              }
              coinCode = @"USDT";

          }
              break;
        case ShellCurrencyTypeBTC:{

                   if(self.wallertModel.address.length > 0){
                       [self.reloadSubject sendNext:@(3)];
                       return;
                   }
                   coinCode = @"BTC";

               }
                   break;
            case ShellCurrencyTypeShell:{

                  if(self.wallertModel.address.length > 0){
                      [self.reloadSubject sendNext:@(3)];
                      return;
                  }
                  coinCode = @"SHELL";

              }
                  break;

        default:
            break;
    }

    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kGetWalletAddress params:@{@"coinCode":coinCode} success:^(id  _Nullable responseObject) {
      @strongify(self);
      if([responseObject[kResultStatus] isEqualToString:kCode]){
          self.wallertModel.address = [NSString stringWithFormat:@"%@",responseObject[kResultData]];
      }
      [self.reloadSubject sendNext:@(3)];
      
    } failure:^(NSError * _Nullable error) {
      @strongify(self);
      [self.reloadSubject sendNext:@(3)];
      
    } isLoading:YES isFailTip:YES];
 
}


/**
 Convert 充值记录
 @param  isFirst 是否是第一次
 */
- (void)getRechargeRecord:(BOOL)isFirst{
    
    
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
    

    NSDictionary *params = @{@"busiCode":@"charge",
                             @"coinCode":self.wallertModel.coinCode?:@""};
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kQueryWalletDetail,(long)self.pageIndex];
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:url params:params success:^(id  _Nullable responseObject) {
        @strongify(self);
        NSArray *dataArray = responseObject[kResultData];
        
        for (int i = 0; i < dataArray.count; i++) {
            ShellWallertModel *wallertModel = [ShellWallertModel mj_objectWithKeyValues:dataArray[i]];
            [self.dataArray addObject:wallertModel];
        }
        //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
        NSInteger totalCount = [responseObject[@"total"] integerValue];
        if(self.dataArray.count == totalCount && self.dataArray.count > 0){
            //没有更多数据
            [self.reloadSubject sendNext:@(0)];
            return;
        }
        //看是否有数据
        if(self.dataArray.count == 0){
            [self.reloadSubject sendNext:@(0)];
        } else {
            self.pageIndex++;
            [self.reloadSubject sendNext:@(1)];
        }
        
    } failure:^(NSError * _Nullable error) {
        
        [self.reloadSubject sendNext:@(0)];
        
    } notwork:^(NSError * _Nullable error) {
        [self.reloadSubject sendNext:@(0)];
        
    } isLoading:NO isFailTip:YES];

    
    
    
}
@end
